<template>
    <div class="app-container">
      <!-- 商家介绍 -->
      <el-card class="box-card">
        <h3>商家编辑</h3>
        <el-form ref="business" :rules="rules" :model="business" label-width="150px">

          <el-form-item label="商家名称" prop="businessName">
            <el-input v-model="business.businessName" />
          </el-form-item>
          <el-form-item label="商家地址" prop="businessAddress">
            <el-input v-model="business.businessAddress" />
          </el-form-item>
          <el-form-item label="商家图片">
            <el-upload
            action="http://localhost:9600/common/upload" list-type="picture" :show-file-list="false"
            :on-success="uploadPicUrl" class="avatar-uploader" accept=".jpg,.jpeg,.png,.gif"
            >
              <img v-if="business.businessImg" :src="business.businessImg" class="avatar" @click="editImg()" />
              <i v-else class="el-icon-plus avatar-uploader-icon" />
            </el-upload>
          </el-form-item>

          <el-form-item label="点餐分类">
            <el-input v-model="business.orderTypeId"> </el-input>
          </el-form-item>

          <el-form-item label="起送费">
            <el-input v-model="business.starPrice"> </el-input>
          </el-form-item>
          <el-form-item label="配送费">
            <el-input v-model="business.deliveryPrice"> </el-input>
          </el-form-item>
          <el-form-item label="商家备注">
            <el-input v-model="business.remarks"> </el-input>
          </el-form-item>

          <el-form-item label="商家介绍">
            <editor :init="editorInit" v-model="business.businessExplain" />
          </el-form-item>
        </el-form>
      </el-card>

      <!-- 更新取消按钮 -->
      <div class="op-container">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleEdit">更新商家</el-button>
      </div>
    </div>
  </template>

  <style>
  .el-card {
    margin-bottom: 10px;
  }
  .el-tag + .el-tag {
    margin-left: 10px;
  }
  .input-new-keyword {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #20a0ff;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 120px;
    line-height: 120px;
    text-align: center;
  }
  .avatar {
    width: 145px;
    height: 145px;
    display: block;
  }
  </style>

  <script>
  import { detailbusiness,editbusiness } from "../../api/business/bussiness";
  import { createStorage, uploadPath } from "@/api/system/storage";
  import Editor from "@/components/TEditor.vue";
  import { MessageBox } from "element-ui";
  import { getToken } from "@/utils/auth";

  export default {
    name: "businessEdit",
    components: { Editor },
    data() {
      return {
        uploadPath, // 图片上传路径
        business: { gallery: [] }, // 商家信息
        // 表单验证规则
        rules: {
          businessId: [
            { required: true, message: "商家编号不能为空", trigger: "blur" },
          ],
          businessName: [
            { required: true, message: "商家名称不能为空", trigger: "blur" },
          ],
        },
        // 富文本编辑器初始化
        editorInit: {
          language: "zh_CN",
          convert_urls: false,
          plugins: [
            "advlist anchor autolink autosave code codesample colorpicker colorpicker contextmenu directionality emoticons fullscreen hr image imagetools importcss insertdatetime link lists media nonbreaking noneditable pagebreak paste preview print save searchreplace spellchecker tabfocus table template textcolor textpattern visualblocks visualchars wordcount",
          ],
          toolbar: [
            "searchreplace bold italic underline strikethrough alignleft aligncenter alignright outdent indent  blockquote undo redo removeformat subscript superscript code codesample",
            "hr bullist numlist link image charmap preview anchor pagebreak insertdatetime media table emoticons forecolor backcolor fullscreen",
          ],
          // 上传钩子
          images_upload_handler: function (blobInfo, success, failure) {
            const formData = new FormData();
            formData.append("file", blobInfo.blob());
            createStorage(formData)
              .then((res) => {
                success(res.data.data.url);
              })
              .catch(() => {
                failure("上传失败，请重新上传");
              });
          },
        },
      };
    },
    computed: {
      // 上传请求头
      headers() {
        return {
          "Shopping-Admin-Token": getToken(),
        };
      },
    },
    created() {
      // 页面数据初始化
      this.init();
    },
    methods: {
      // 获取数据 ，判断是否有商家id传入
      init: function () {
        if (this.$route.query.id == null) {
          return;
        }


        const businessId = this.$route.query.id;

        detailbusiness(businessId).then((response) => {
          this.business = response.data;
        });


      },
      editImg(){
      if(this.business.businessImg!='' || this.business.businessImg!=undefined || this.business.businessImg!=null){
        this.business.businessImg = null;
        console.log();


      }

    },
      handleCancel(){
        this.$router.push({ path: "/business/index" });
      },

      // 更新商家钩子
      handleEdit: function () {
        // 构建提交数据
        const finalbusiness = {
          business: this.business,
          specifications: this.specifications,
          products: this.products,
          attributes: this.attributes,
        };
        // 调用接口
        editbusiness(this.business)
          .then((response) => {
            this.$notify.success({
              title: "成功",
              message: "更新成功",
            });
            this.$router.push({ path: "/business/index" });
          })
          .catch((response) => {
            MessageBox.alert("业务错误：" + response.data.errmsg, "警告", {
              confirmButtonText: "确定",
              type: "error",
            });
          });
      },

      // 获取图片上传路径
      uploadPicUrl: function (response) {
        this.business.businessImg = response.data;
      },
      // 限制上文件个数
      uploadOverrun: function () {
        this.$message({
          type: "error",
          message: "上传文件个数超出限制!最多上传5张图片!",
        });
      },
      // 获取图片路径
      handleGalleryUrl(response, file, fileList) {
        if (response.errno === 0) {
          this.business.gallery.push(response.data.url);
        }
      },

      // 上传成功钩子
      uploadProductUrl: function (response) {
        this.productForm.url = response.data.url;
      }
    },
  };
  </script>
